<?php

namespace App\Http\Controllers\Admin\Statistics;

use App\Http\Controllers\Admin\BaseController;
use App\Repositories\UserRepositoryEloquent;

use App\User;
use App\Models\Option;
use App\Models\Order;
use App\Repositories\MemberRepositoryEloquent;
use App\Models\Recharge;
use App\Models\Withdrawals;
use App\Models\Commission;
use Breadcrumbs, Toastr,Config,Validator;
use Illuminate\Http\Request;
use App\Models\FinancialDetails;

class SummaryController extends BaseController{

    protected $user;

    public function __construct(MemberRepositoryEloquent $user)
    {
        parent::__construct();
        $this->user = $user;


    }

    public function index(Request $request){

        $where=[];
        $whereHas=[];
        $data=['status'=>-1,'name'=>'','type'=>'','mobile'=>'','from'=>'','to'=>''];

        if(!empty($request->all())){
            //if($request->name){
            //dd($request->all());
            $datas = $request->all();
            foreach($datas as $k=>$v){
                if(($k=='name' || $k=='mobile') && $v!=''){
                    $whereHas[] = ["$k",'like','%'.$v.'%'];
                }
                if($k=='from' && $v!=''){
                    //$where[] = ['created_at','>',strtotime($v)];
                    $where[] = ['created_at','>',$v];
                }
                if($k=='to' && $v!=''){
                    //$where[] = ["created_at",'<',strtotime($v)];
                    $where[] = ["created_at",'<',$v];
                }
                if($k=='status' && $v!='' && $v!=-1){
                    $where[] = ["$k",'=',$v];
                }
                $data[$k] =$v;
                
            }
            //dd($where);
            //$users = User::where($where)->paginate(20);
        }

        //用户总数
        $users_count = User::where($whereHas)->where($where)->count();
        //总余额
        $total_balance = User::where($whereHas)->where($where)->sum('balance');
        //总单数
        $orders_count = Order::whereHas('user',function($query) use($whereHas){
                                $query->where($whereHas);
                        })->where($where)->count();
        //总收入
        $total_profit = 0;
        //echo $users_count,'-',$total_balance,'-',$orders_count;
        $total_transaction_cost = 0;

        //订单查询
        //1.查出有哪些产品
        $options = Option::get();
        foreach($options as $k=>$option){
                //总单数
                $count = Order::whereHas('user',function($query) use($whereHas){
                                $query->where($whereHas);
                        })->where($where)->where('option_id','=',$option->id)->count();
                
                //保证金总额
                $total_margin = Order::whereHas('user',function($query) use($whereHas){
                                $query->where($whereHas);
                        })->where($where)->where('option_id','=',$option->id)->sum('stop_loss_margin')*$option->exchange_rate;
                //综合费用
                $total_cost = Order::whereHas('user',function($query) use($whereHas){
                                $query->where($whereHas);
                        })->where($where)->where('option_id','=',$option->id)->sum('transaction_cost')*$option->exchange_rate;
                //用户盈利
                $total_earn = Order::whereHas('user',function($query) use($whereHas){
                                $query->where($whereHas);
                        })->where($where)->where('option_id','=',$option->id)->where('profit','>',0)->sum('profit');
                //用户亏损
                $total_loss = Order::whereHas('user',function($query) use($whereHas){
                                $query->where($whereHas);
                        })->where($where)->where('option_id','=',$option->id)->where('profit','<',0)->sum('profit');
                $options[$k]['orders_count'] = $count;
                $options[$k]['total_margin'] = $total_margin;
                $options[$k]['total_cost'] = $total_cost;
                $total_transaction_cost+=$total_cost;
                $options[$k]['total_earn'] = $total_earn;
                $options[$k]['total_loss'] = $total_loss;
                $options[$k]['total_profit'] = (-$total_loss)-$total_earn;
                $total_profit += $options[$k]['total_profit'];
        }       

        $total_rechages = Recharge::where($where)->where('status','=',1)
                        ->whereHas('user',function($query) use($whereHas){
                            $query->where($whereHas);
                        })->sum('money');
        $total_withdrawals = Withdrawals::where($where)
                        ->whereHas('user',function($query) use($whereHas){
                            $query->where($whereHas);
                        })->sum('money');
        $total_withdrawals_fee = Withdrawals::where($where)
                        ->whereHas('user',function($query) use($whereHas){
                            $query->where($whereHas);
                        })->sum('fee');
        $commissions = Commission::where($where)
                        ->whereHas('commission_user',function($query) use($whereHas){
                            $query->where($whereHas);
                        })->sum('commission');
        return view('admin.statistics.summary',compact('users_count','total_balance','orders_count','total_profit','total_transaction_cost','options','total_rechages','total_withdrawals','total_withdrawals_fee','commissions','data'));
    }

}